package com.jzr.bedside.api

import com.google.gson.Gson
import com.jzr.bedside.base.Constant
import com.jzr.bedside.bean.*
import com.jzr.bedside.bean.body.AddSatisfyBean
import com.jzr.bedside.bean.body.RecordBody
import com.jzr.bedside.bean.body.TemperatureAddBody
import com.jzr.bedside.utils.CommonUtil
import okhttp3.OkHttpClient
import okhttp3.RequestBody
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.QueryMap
import rx.Observable
import java.util.*


class Api(okHttpClient: OkHttpClient) {

    init {
        val retrofit: Retrofit = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(Gson()))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .client(okHttpClient)
            .baseUrl(CommonUtil.getBaseUrl())
            .build()
        service =
            retrofit.create(ApiService::class.java)
    }

    companion object {
        private var instance: Api? = null
        private var service: ApiService? = null
        fun getInstance(): Api? {
            synchronized(Api::class.java) {
                if (instance == null) {
                    instance =
                        Okhttp.provideOkHttpClient()?.let { Api(it) }
                }
                return instance
            }
        }

        fun getMap(
            vararg s: String
        ): Map<String, String>? {
            val map: MutableMap<String, String> =
                HashMap()
            if (s.size % 2 != 0) {
                return null
            } else {
                var i = 0
                while (i < s.size) {
                    map[s[i]] = s[++i]
                    i++
                }
            }
            return map
        }
    }

    //连接测试
    fun connectTest(): rx.Observable<ConnectTestBean?>? {
        return service?.connectTest()
    }

    //绑定床位号
    fun getBindingBedNo( s: RecordBody?): rx.Observable<BindingBedBean?>? {
        return service?.getBindingBedNo(s!!)
    }

    //选择床位号
    fun getCheckBedNo(vararg s: String?): rx.Observable<CheckBedListBean?>? {
        return service?.getCheckBedNo(getMap(*s as Array<out String>)!!)
    }


    //选择科室
    fun getCheckDeptNo(): rx.Observable<DeptCodeListBean?>? {
        return service?.getCheckDeptNo()
    }

    // 获取病人信息
    fun getBedInfo(vararg s: String?): rx.Observable<BedInfoBean?>? {
        return service?.getBedInfo(getMap(*s as Array<out String>)!!)
    }

    // 获取设备信息
    fun getDeviceInfo(vararg s: String?): rx.Observable<DeviceInfoBean?>? {
        return service?.getDeviceInfo(getMap(*s as Array<out String>)!!)
    }

    // 获取多媒体列表
    fun getMediaList(vararg s: String?): rx.Observable<MediaListBean?>? {
        return service?.getMediaList(getMap(*s as Array<out String>)!!)
    }

    // 获取满意度列表
    fun getSatisfySurveyList(vararg s: String?): rx.Observable<TQuestionBean?>? {
        return service?.getSatisfySurveyList(getMap(*s as Array<out String>)!!)
    }

    // 添加满意度列表
    fun getBedsideSatisfySurveyAdd(boby: RequestBody?): rx.Observable<TQuestionBean?>? {
        return service?.getBedsideSatisfySurveyAdd(boby!!)
    }

    // 获取最新版本
    fun getLastVersion(vararg s: String?): rx.Observable<LastVersionBean?>? {
        return service?.getLastVersion(getMap(*s as Array<out String>)!!)
    }

    // 获取医院简介
    fun getHospitalIntro(vararg s: String?): rx.Observable<HospitalIntroBean?>? {
        return service?.getHospitalIntro(getMap(*s as Array<out String>)!!)
    }

    // 获取科室简介
    fun getDeptIntro(vararg s: String?): rx.Observable<DeptIntroBean?>? {
        return service?.getDeptIntro(getMap(*s as Array<out String>)!!)
    }

    // 获取费用明细列表
    fun getFeeRecordList(vararg s: String?): rx.Observable<FeeRecordListBean?>? {
        return service?.getFeeRecordList(getMap(*s as Array<out String>)!!)
    }

    // 获取检查报告列表
    fun getTestReportList(vararg s: String?): rx.Observable<TestReportListBean?>? {
        return service?.getTestReportList(getMap(*s as Array<out String>)!!)
    }

    // 获取检验报告列表
    fun getInspectReportList(vararg s: String?): rx.Observable<InspectReportListBean?>? {
        return service?.getInspectReportList(getMap(*s as Array<out String>)!!)
    }

    // 获取医嘱列表
    fun getOrderList(vararg s: String?): rx.Observable<OrderListBean?>? {
        return service?.getOrderList(getMap(*s as Array<out String>)!!)
    }

    // 添加温度
    fun getTemperatureAdd(body: RequestBody?): rx.Observable<TemperatureBean?>? {
        return body?.let { service?.getTemperatureAdd(it) }
    }

    // 添加血氧
    fun getOxymeterAdd(body: RequestBody?): rx.Observable<TemperatureBean?>? {
        return body?.let { service?.getOxymeterAdd(it) }
    }

    // 添加血压
    fun getBloodPressAdd(body: RequestBody?): rx.Observable<TemperatureBean?>? {
        return body?.let { service?.getBloodPressAdd(it) }
    }

    // 呼叫
    fun getCall(dto: RequestBody?): rx.Observable<TemperatureBean?>? {
        return dto?.let { service?.getCall(it) }
    }



}